<template>
  <el-form ref="formLabelAlign"  :model="formLabelAlign"   :rules="rules" label-width="140px">
    <el-row>
      <el-col :span="10">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="formLabelAlign.username" />
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="formLabelAlign.name" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="10">
        <el-form-item label="用户状态" prop="state">
          <el-select v-model="formLabelAlign.state" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="启用" value="0" />
            <el-option label="禁用" value="1" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="formLabelAlign.sex" placeholder="请选择">
            <el-option label="请选择" value="" />
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
          </el-select>
        </el-form-item>
      </el-col>

    </el-row>
    <el-row>
      <el-col :span="10">
        <el-form-item label="手机号码" prop="phone">
          <el-input  v-model="formLabelAlign.phone"   />
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="电子邮件" prop="email">
          <el-input  v-model="formLabelAlign.email"   />
        </el-form-item>
      </el-col>
    </el-row>



    <el-row>
      <el-col :span="10">
        <el-form-item label="用户头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess" >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-row>


    <el-button @click="save('formLabelAlign')" type="primary" size="mini"  >确 定</el-button>
    <el-button @click="resetForm('formLabelAlign')"  size="mini"  >重 置</el-button>
    <el-button  size="mini" @click="cancel" type="danger">关 闭</el-button>


  </el-form>
</template>

<script>

import { findUserById } from '@/api/user'

export default {
  name: 'UserAdd',
  props: ['editId'],
  data() {
    return {
      rules: {
        username:[
          { required: true, message: '请输入用户名称', trigger: 'change' }

        ],
        name:[
          { required: true, message: '请输入用户姓名', trigger: 'change' }
        ],
        state:[
          { required: true, message: '请选择用户状态', trigger: 'change' }
        ],
        sex:[
          { required: true, message: '请选择用户性别', trigger: 'change' }
        ]
      },
      passwordType: 'password',
      formLabelAlign: {
        username: '',
        name: '',
        email: '',
        phone:'',
        state: '',
        id: '',
        sex:''
      },
      id: 0,
      imageUrl: ''
    }
  },
  watch: {
    editId() {
      this.id = this.editId
      this.loadData(this.id)
    }
  },
  mounted() {
    this.id = this.editId
    console.log('id', this.id)
    this.loadData(this.id)
  },
  methods: {
    loadData(id) {
      if (id == null || id === '') {
        return false
      }
      findUserById(id).then(response => {
        console.log('response', response)
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    submitForm(){

    },
    resetForm(){

    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
